<%@Page Language="VB"%>
<%@Import Namespace="System.Xml" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head>
<title>Creating and Editing the Content of XML Documents</title>
<!-- #include file="..\global\style.inc" -->
</head>
<body bgcolor="#ffffff">
<span class="heading">Creating and Editing the Content of XML Documents</span><hr />
<!--------------------------------------------------------------------------->

<div id="outDocURL" runat="server"></div><p />
<div id="outResult1" runat="server"></div><p />
<div id="outResult2" runat="server"></div><p />
<div id="outResult3" runat="server"></div><p />
<div id="outError" runat="server"></div>

<script language="vb" runat="server">

Sub Page_Load()

   'create physical path to booklist sample files (in same folder as ASPX page)
   Dim strCurrentPath As String = Request.PhysicalPath
   Dim strXMLPath As String = Left(strCurrentPath, InStrRev(strCurrentPath, "\")) & "bookdetails.xml"
   Dim strNewPath As String = Left(strCurrentPath, InStrRev(strCurrentPath, "\")) & "newdetails.xml"

   'create a new XMLDocument object
   Dim objXMLDoc As New XMLDocument()

   Try

      'load the XML file into the XMLDocument object
      objXMLDoc.Load(strXMLPath)
      outDocURL.innerHTML = "<b>* Loaded file</b>: <a href=""" & strXMLPath _
                          & """>" & strXMLPath & "</a><br />"

   Catch objError As Exception

      'display error details
      outError.innerHTML = "<b>* Error while accessing document</b>.<br />" _
          & objError.Message & "<br />" & objError.Source
      Exit Sub  ' and stop execution

   End Try

   'now ready to use the XMLDocument object

   '*************************************************************
   '*** 1: Select a Node, display content and then remove it  ***
   '*************************************************************

   'specify XPath expression to select a book elment
   Dim strXPath As String = "descendant::Book[ISBN=" & Chr(34) _
                          & "1861003234" & Chr(34) & "]"

   'get a reference to the matching <Book> node
   Dim objNode As XmlNode
   objNode = objXMLDoc.SelectSingleNode(strXPath)

   'then display the result
   outResult1.InnerHtml = "XPath expression '<b>" & strXPath _
                        & "</b>' returned:<br />" _
                        & Server.HtmlEncode(objNode.OuterXml) & "<br />"

   'delete this node using RemoveChild method from document element
   objXMLDoc.DocumentElement.RemoveChild(objNode)
   outResult1.InnerHtml += "<b>* Removed node from document.</b><br />"

   '******************************************************************
   '*** 2: Create empty XML document, add declaration and comment  ***
   '******************************************************************

   'create new empty XML Document object
   Dim objNewDoc As New XmlDocument()

   'create a new XmlDeclaration object
   Dim objDeclare As XmlDeclaration
   objDeclare = objNewDoc.CreateXmlDeclaration("1.0", Nothing, Nothing)

   'and add it as the first node in the new document
   objDeclare = objNewDoc.InsertBefore(objDeclare, objNewDoc.DocumentElement)

   'create a new XmlComment object
   Dim objComment As XmlComment
   objComment = objNewDoc.CreateComment("New document created " & Now())

   'and add it as the second node in the new document
   objComment = objNewDoc.InsertAfter(objComment, objDeclare)

   '********************************************************************
   '*** 3: Select node in original document and import into new one  ***
   '********************************************************************

   'change the XPath expression to select a different book
   strXPath = "descendant::Book[ISBN=" & Chr(34) & "1861003382" & Chr(34) & "]"

   'get a reference to the matching <Book> node
   objNode = objXMLDoc.SelectSingleNode(strXPath)

   'create a variable to hold the imported node object
   Dim objImportedNode As XmlNode

   'import the node and all children into new document (un-attached fragment)
   objImportedNode = objNewDoc.ImportNode(objNode, True)

   'insert the new un-attached node into document after the comment node
   objNewDoc.InsertAfter(objImportedNode, objComment)

   'display the contents of the new document
   outResult2.InnerHtml = "Created new XML document and inserted " _
                        & "into it the node selected by<br />" _
                        & "the XPath expression '<b>" & strXPath & "'</b><br />" _
                        & "<b>* Content of new document is</b>:<br />" _
                        & Server.HtmlEncode(objNewDoc.OuterXml)

   '*****************************************************************
   '*** 4: Select and edit/insert new content into ISBN elements  ***
   '*****************************************************************

   'change the XPath expression to select all ISBN elements
   strXPath = "descendant::ISBN"

   'get a reference to the matching nodes as a collection
   Dim colNodeList As XmlNodeList
   colNodeList = objXMLDoc.SelectNodes(strXPath)

   'display the number of matches found
   outResult3.InnerHtml = "Found <b>" & colNodeList.Count & "</b> nodes matching the" _
                        & "XPath expression '<b>" & strXPath & "'</b><br />" _
                        & "<b>* Editing and inserting new content</b><br />"

   Dim strNodeValue, strNewValue, strShortCode As String

   'create a variable to hold an XmlAttribute object
   Dim objAttr As XmlAttribute

   'iterate through all the nodes found
   For Each objNode In colNodeList

     'create an XmlAttribute named 'formatting'
     objAttr = objXMLDoc.CreateAttribute("formatting")

     'set the value of the XmlAttribute to 'hyphens'
     objAttr.Value = "hyphens"

     'and add it to this ISBN element - have to cast the object
     'to an XmlElement as XmlNode doesn't have this method
     CType(objNode, XmlElement).SetAttributeNode(objAttr)

     'get text value of this ISBN element
     strNodeValue = objNode.InnerText

     'create short and long strings to replace content
     strShortCode = Right(strNodeValue, 4)
     strNewValue = Left(strNodeValue, 1) & "-" & Mid(strNodeValue, 2, 6) _
                 & "-" & Mid(strNodeValue, 8, 2) & "-" & Right(strNodeValue, 1)

     'insert into element by setting the InnerXml property
     objNode.InnerXml = "<LongCode>" & strNewValue & "</LongCode><ShortCode>" _
                      & strShortCode & "</ShortCode>"

   Next

   'write the updated document to a disk file
   objXMLDoc.Save(strNewPath)

   'display a link to view the updated document
   outResult3.InnerHTML += "<br /><b>* Saved updated document</b>: <a href=""" _
                        & strNewPath & """>" & strNewPath & "</a><br />"


End Sub

</script>

<!--------------------------------------------------------------------------->
<!-- #include file="..\global\foot.inc" -->
</body>
</html>
